package com.example.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.home.activity.R;
import com.example.home.viewmodel.cart.CartList;

import java.util.List;

public class CartListAdapter extends BaseAdapter {
    private Context ctx;
    private List<CartList> cartLists;

    public CartListAdapter(Context ctx, List<CartList> cartLists) {
        this.ctx = ctx;
        this.cartLists = cartLists;
    }

    @Override
    public int getCount() {
        return cartLists.size();
    }

    @Override
    public Object getItem(int position) {
        return cartLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return cartLists.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(this.ctx).inflate(R.layout.cart_list_ltem,null);
            viewHolder.cart_list_img = convertView.findViewById(R.id.cart_list_img);
            viewHolder.cart_list_goodsName = convertView.findViewById(R.id.cart_list_goodsName);
            viewHolder.cart_list_text = convertView.findViewById(R.id.cart_list_text);
            viewHolder.cart_list_price = convertView.findViewById(R.id.cart_list_price);
            viewHolder.cart_list_number = convertView.findViewById(R.id.cart_list_number);
            viewHolder.cart_list_addTime = convertView.findViewById(R.id.cart_list_addTime);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.cart_list_goodsName.setText(cartLists.get(position).getGoodsName());
        viewHolder.cart_list_price.setText(cartLists.get(position).getPrice().toString());
        viewHolder.cart_list_number.setText(cartLists.get(position).getNumber()+"");
        viewHolder.cart_list_addTime.setText(cartLists.get(position).getAddTime());
        Glide.with(convertView.getContext())
                .load(cartLists.get(position).getPicUrl())
                .into(viewHolder.cart_list_img);
        return convertView;
    }

    static class ViewHolder {
        ImageView cart_list_img;
        TextView cart_list_goodsName;
        TextView cart_list_text;
        TextView cart_list_price;
        TextView cart_list_number;
        TextView cart_list_addTime;
    }
}
